package com.spell.joy.sdk.ad;

import android.app.Activity;
import android.util.Log;

import androidx.annotation.NonNull;

import com.applovin.mediation.MaxAd;
import com.applovin.mediation.MaxError;
import com.applovin.mediation.MaxReward;
import com.applovin.mediation.MaxRewardedAdListener;
import com.applovin.mediation.ads.MaxRewardedAd;
import com.spell.joy.sdk.JoyConstant;
import com.spell.joy.sdk.JoySDKManager;
import com.spell.joy.sdk.interfaces.JoyVideoIAP;


public class JoyMaxReward implements JoyVideoIAP {
    private MaxRewardedAd maxRewardedAd;
    private JoySDKManager.JoyVideoCallback mlistner;

    public static JoyMaxReward getInstance() {
        return InstanceHolder.JOY_MAX_REWARD;
    }

    private static class InstanceHolder {
        private static final JoyMaxReward JOY_MAX_REWARD = new JoyMaxReward();
    }

    public void loadRewardAd() {
        maxRewardedAd =MaxRewardedAd.getInstance(JoyConstant.REWARD_PLACEMENTID);
        mlistner=null;
        setListner();
        loadAd();
    }

    private void loadAd(){
        maxRewardedAd.loadAd();
        Log.e(JoyConstant.TAG,"load max");
    }


    private void setListner() {
        maxRewardedAd.setListener(new AdListener());
    }

    @Override
    public boolean isReady(){
        if (maxRewardedAd != null) {
            return maxRewardedAd.isReady();
        }
        return false;
    }

    @Override
    public void showVideo(Activity activity, JoySDKManager.JoyVideoCallback listner) {
        if (maxRewardedAd != null) {
            mlistner=listner;
            setListner();
            show(activity);
        }else {
            onError();
        }

    }

    private void show(Activity activity) {
        if (isReady()) {
            maxRewardedAd.showAd(activity);
        } else {
            onError();
            loadAd();
        }
    }

    public class AdListener implements MaxRewardedAdListener{

        @Override
        public void onUserRewarded(@NonNull MaxAd maxAd, @NonNull MaxReward maxReward) {

        }

        @Override
        public void onAdLoaded(@NonNull MaxAd maxAd) {
            Log.e(JoyConstant.TAG,"onAdLoaded");
        }

        @Override
        public void onAdDisplayed(@NonNull MaxAd maxAd) {
            Log.e(JoyConstant.TAG,"onAdDisplayed"+maxAd.getNetworkName());
        }

        @Override
        public void onAdHidden(@NonNull MaxAd maxAd) {
            loadAd();
            if (mlistner != null) {
                mlistner.onAdOver();
            }
        }

        @Override
        public void onAdClicked(@NonNull MaxAd maxAd) {

        }

        @Override
        public void onAdLoadFailed(@NonNull String s, @NonNull MaxError error) {
            Log.d(JoyConstant.TAG,"onMaxRewardLoadFailed "+error.getCode()+ error.getMessage()+error.getMediatedNetworkErrorCode()+error.getMediatedNetworkErrorMessage());
        }

        @Override
        public void onAdDisplayFailed(@NonNull MaxAd maxAd, @NonNull MaxError error) {
            Log.e(JoyConstant.TAG,"onAdDisplayFailed "+error.getCode() +error.getMessage()+error.getMediatedNetworkErrorCode()+error.getMediatedNetworkErrorMessage());
            onError();
        }
    }

    private void onError(){
        if (mlistner != null) {
            mlistner.onAdError();
        }
    }
}
